package com.company.cloud.company.client;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.bean.BaseClientResult;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.company.modules.api.request.*;
import com.company.cloud.company.modules.api.response.*;
import com.company.cloud.company.modules.api.service.CompanyRemoteService;
import com.company.cloud.company.modules.base.service.CompanyInfoService;
import com.company.cloud.company.modules.cache.service.CompanyCacheService;
import com.company.cloud.company.modules.cache.utils.CompanyConvertUtils;
import com.company.cloud.company.modules.mgr.service.CompanyMgrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@RestController
@Service
public class CompanyRemoteServiceImpl implements CompanyRemoteService {

    @Autowired
    private CompanyCacheService companyCacheService;
    @Autowired
    private CompanyInfoService companyInfoService;
    @Autowired
    private CompanyMgrService companyMgrService;

    @Override
    public BaseClientResult<CompanySimple> getCompanySimple(String companyNo) {
        try {
            CompanyFull company= companyCacheService.getCompanyByDB(companyNo);
            return BaseClientResult.success(company);
        }catch (Exception e){
            e.printStackTrace();
            return  BaseClientResult.error(e);
        }
    }

    @Override
    public BaseClientResult<Map<String, CompanySimple>> getCompanyMapSimple(Set<String> companyNos) {
        try {
            Map<String, CompanySimple> data=new HashMap<String, CompanySimple>();
            Map<String, CompanyFull> companyMap= companyCacheService.getCompanyMapByDB(companyNos);
            if(!CollectionUtils.isEmpty(companyMap)) {
                companyMap.forEach((k,v)->{
                    data.put(k, CompanyConvertUtils.getCompanySimple(v));
                });
            }
            return BaseClientResult.success(data);
        }catch (Exception e){
            e.printStackTrace();
            return  BaseClientResult.error(e);
        }
    }

    @Override
    public BaseClientResult<CompanyFull> getCompanyFull(String companyNo) {
        try {
            CompanyFull company= companyCacheService.getCompanyByDB(companyNo);
            return BaseClientResult.success(company);
        }catch (Exception e){
            e.printStackTrace();
            return  BaseClientResult.error(e);
        }
    }

    @Override
    public  BaseClientResult<Map<String, CompanyFull>> getCompanyMapFull(Set<String> companyNos) {
        try {
            Map<String, CompanyFull> data=new HashMap<String, CompanyFull>();
            Map<String, CompanyFull> companyMap= companyCacheService.getCompanyMapByDB(companyNos);
            if(!CollectionUtils.isEmpty(companyMap)) {
                companyMap.forEach((k,v)->{
                    data.put(k, v);
                });
            }
            return BaseClientResult.success(data);
        }catch (Exception e){
            e.printStackTrace();
            return  BaseClientResult.error(e);
        }
    }

    @Override
    public BaseClientResult<CompanyFullPage> queryCompanyFull(CompanyQueryRequest request) {
        try {
            IPage<CompanyFull> data=companyMgrService.query(request);
            CompanyFullPage result=new CompanyFullPage();
            result.setRecords(data.getRecords());
            result.setCurrent(data.getCurrent());
            result.setTotal(data.getTotal());
            result.setSize(data.getSize());
            result.setPages(data.getPages());
            return BaseClientResult.success(result);
        }catch  (Exception e){
            e.printStackTrace();
            return  BaseClientResult.error(e);
        }
    }

    @Override
    public BaseClientResult<CompanyFull> addCompany(CompanyAddRequest request) {
        try {
            ValidatorUtils.validate(request);
            LoginSimpleUserInfo userInfo=new LoginSimpleUserInfo();
            userInfo.setId(request.getOpUserId());
            userInfo.setIp(request.getOpIp());
            CompanyFull data= companyMgrService.save(request,userInfo);
            return BaseClientResult.success(data);
        }catch  (Exception e){
            e.printStackTrace();
            return BaseClientResult.error(e);
        }
    }

    @Override
    public BaseClientResult<CompanyFull> addCompany2User(Company2UserAddRequest request) {
        try {
            ValidatorUtils.validate(request);
            LoginSimpleUserInfo userInfo=new LoginSimpleUserInfo();
            userInfo.setId(request.getOpUserId());

            if(StringUtils.isEmpty(request.getOpIp())){
                userInfo.setIp("127.0.0.1");
            }else{
                userInfo.setIp(request.getOpIp());
            }
            CompanyFull data=companyMgrService.save2user(request,userInfo);
            return BaseClientResult.success(data);
        }catch  (Exception e){
            e.printStackTrace();
            return BaseClientResult.error(e);
        }
    }

    @Override
    public BaseClientResult<CompanyFull> updateCompany(CompanyUpdateRequest request) {
        try {
            ValidatorUtils.validate(request);
            LoginSimpleUserInfo userInfo=new LoginSimpleUserInfo();
            userInfo.setId(request.getOpUserId());
            userInfo.setIp(request.getOpIp());
            CompanyFull data=companyMgrService.update(request,userInfo);
            return BaseClientResult.success(data);
        }catch  (Exception e){
            e.printStackTrace();
            return BaseClientResult.error(e);
        }
    }

    @Override
    public BaseClientResult<CompanyFull> delCompany(CompanyDelRequest request) {
        try {
            ValidatorUtils.validate(request);
            LoginSimpleUserInfo userInfo=new LoginSimpleUserInfo();
            userInfo.setId(request.getOpUserId());
            userInfo.setIp(request.getOpIp());
            CompanyFull data=companyMgrService.del(request.getCompanyNo(),userInfo);
            return BaseClientResult.success(data);
        }catch  (Exception e){
            e.printStackTrace();
            return BaseClientResult.error(e);
        }
    }

    @Override
    public BaseClientResult<CompanyFull> checkCompany(CompanyCheckRequest request) {
        try {
            ValidatorUtils.validate(request);
            LoginSimpleUserInfo userInfo=new LoginSimpleUserInfo();
            userInfo.setId(request.getOpUserId());
            userInfo.setIp(request.getOpIp());
            CompanyFull data=companyMgrService.check(request,userInfo);
            return BaseClientResult.success(data);
        }catch  (Exception e){
            e.printStackTrace();
            return BaseClientResult.error(e);
        }
    }

    @Override
    public BaseClientResult<CompanyFull> frozenCompany(CompanyFrozenRequest request) {
        try {
            ValidatorUtils.validate(request);
            LoginSimpleUserInfo userInfo=new LoginSimpleUserInfo();
            userInfo.setId(request.getOpUserId());
            userInfo.setIp(request.getOpIp());
            CompanyFull data=companyMgrService.frozen(request.getCompanyNo(),userInfo);
            return BaseClientResult.success(data);
        }catch  (Exception e){
            e.printStackTrace();
            return  BaseClientResult.error(e);
        }
    }

    @Override
    public BaseClientResult<CompanyFull> unfreezeCompany(CompanyUnFreezeRequest request) {
        try {
            ValidatorUtils.validate(request);
            LoginSimpleUserInfo userInfo=new LoginSimpleUserInfo();
            userInfo.setId(request.getOpUserId());
            userInfo.setIp(request.getOpIp());
            CompanyFull data=companyMgrService.unfreeze(request.getCompanyNo(),userInfo);
            return BaseClientResult.success(data);
        }catch  (Exception e){
            e.printStackTrace();
            return  BaseClientResult.error(e);
        }
    }
}
